package com.zhonghan.recharge.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.ZhBusinessRecordDao;
import com.zhonghan.recharge.dao.ZhBusinessUserDao;
import com.zhonghan.recharge.pojo.ZhBusinessRecord;
import com.zhonghan.recharge.pojo.ZhBusinessUser;
import com.zhonghan.recharge.service.ZhBusinessRecordService;

@Service
public class ZhBusinessRecordServiceImpl implements ZhBusinessRecordService {
	@Autowired
	private ZhBusinessRecordDao zhBusinessRecordDao;
	@Autowired
	private ZhBusinessUserDao zhBusinessUserDao;

	@Override
	public long getZhBusinessRecordRowCount() {
		return zhBusinessRecordDao.getZhBusinessRecordRowCount();
	}

	@Override
	public List<ZhBusinessRecord> selectZhBusinessRecord() {
		return zhBusinessRecordDao.selectZhBusinessRecord();
	}

	@Override
	public ZhBusinessRecord selectZhBusinessRecordByObj(ZhBusinessRecord obj) {
		return zhBusinessRecordDao.selectZhBusinessRecordByObj(obj);
	}

	@Override
	public ZhBusinessRecord selectZhBusinessRecordById(Integer id) {
		return zhBusinessRecordDao.selectZhBusinessRecordById(id);
	}

	@Override
	public int insertZhBusinessRecord(ZhBusinessRecord value) {
		return zhBusinessRecordDao.insertZhBusinessRecord(value);
	}

	@Override
	public int insertNonEmptyZhBusinessRecord(ZhBusinessRecord value) {
		return zhBusinessRecordDao.insertNonEmptyZhBusinessRecord(value);
	}

	@Override
	public int insertZhBusinessRecordByBatch(List<ZhBusinessRecord> value) {
		return zhBusinessRecordDao.insertZhBusinessRecordByBatch(value);
	}

	@Override
	public int deleteZhBusinessRecordById(Integer id) {
		return zhBusinessRecordDao.deleteZhBusinessRecordById(id);
	}

	@Override
	public int updateZhBusinessRecordById(ZhBusinessRecord enti) {
		return zhBusinessRecordDao.updateZhBusinessRecordById(enti);
	}

	@Override
	public int updateNonEmptyZhBusinessRecordById(ZhBusinessRecord enti) {
		return zhBusinessRecordDao.updateNonEmptyZhBusinessRecordById(enti);
	}

	public ZhBusinessRecordDao getZhBusinessRecordDao() {
		return this.zhBusinessRecordDao;
	}

	public void setZhBusinessRecordDao(ZhBusinessRecordDao zhBusinessRecordDao) {
		this.zhBusinessRecordDao = zhBusinessRecordDao;
	}

	public List<ZhBusinessRecord> selectZhBusinessRecordByUserId(Long id) {
		return zhBusinessRecordDao.selectZhBusinessRecordByUserId(id);
	}

	public Result selectZhBusinessRecordByTime(Long userId, String beginTime, String endTime, Integer pageNum,
			Integer pageSize) {
		pageNum = pageNum == null ? 0 : pageNum;
		pageSize = pageSize == null ? 0 : pageSize;

		ZhBusinessRecord zhBusinessRecord = new ZhBusinessRecord();
		if(userId != null){
			zhBusinessRecord.setUserId(userId);
		}
		if (StringUtils.isNotEmpty(beginTime)) {
			zhBusinessRecord.setBeginTime(beginTime);
		}
		if (StringUtils.isNotEmpty(endTime)) {
			zhBusinessRecord.setEndTime(endTime);
		}

		zhBusinessRecord.setOrderBy("recharge_time");
		zhBusinessRecord.setSortBy("desc");

		Result result = new Result();
		try {

			PageHelper.startPage(pageNum, pageSize);
			List<ZhBusinessRecord> selectZhBusinessRecordByTime = zhBusinessRecordDao
					.selectZhBusinessRecordByTime(zhBusinessRecord);
			
			PageInfo<ZhBusinessRecord> pageInfo = new PageInfo<>(selectZhBusinessRecordByTime);
			
			//查到昵称 封装进去
			for (ZhBusinessRecord zhBusinessRecord2 : selectZhBusinessRecordByTime) {
				 ZhBusinessUser selectZhBusinessUserById = zhBusinessUserDao.selectZhBusinessUserById(userId);
				 zhBusinessRecord2.setNickName(selectZhBusinessUserById.getNickname());
			}
			
			result.setCode(ResultStatus.SUCCESS.getCode());
			result.setData(pageInfo);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
		}

		return result;
	}
}